package com.ganzi.ganziserver.conf.rabbit;

import com.ganzi.ganziserver.dao.ControlLogDao;
import com.ganzi.ganziserver.dao.SchoolInformationDao;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

public class RabbitReturnCallBack implements RabbitTemplate.ReturnCallback{

    Logger log = LoggerFactory.getLogger(RabbitReturnCallBack.class);

    @Autowired
    ControlLogDao logdao;

    @Autowired
    SchoolInformationDao schooldao;


    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        String correlationId = message.getMessageProperties().getCorrelationId();
        String error = "交换机中队列键不存在 交换机："+exchange+" 路由键:"+routingKey;
        try{
            logdao.updateControlLogStateByInfoId(2,error,correlationId);
            if(StringUtils.isNotBlank(routingKey))schooldao.updateSchoolForOnline(routingKey,2);
            log.info("消息：{} 发送失败, 应答码：{} 原因：{} 交换机: {}  路由键: {}", correlationId, replyCode, replyText, exchange, routingKey);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
